package hierarchy

import (
	"errors"
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/hierarchy"
	hierarchyReq "gitcode.com/G-YT/iHealth25/server/model/hierarchy/request"
	"gorm.io/gorm"
)

type LevelOneService struct {
}

// CreateLevelOne 创建LevelOne记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelOneService *LevelOneService) CreateLevelOne(levelOne *hierarchy.LevelOne) (err error) {
	err = global.GVA_DB.Create(levelOne).Error
	return err
}

// DeleteLevelOne 删除LevelOne记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelOneService *LevelOneService) DeleteLevelOne(levelOne hierarchy.LevelOne) (err error) {
	if !errors.Is(global.GVA_DB.Where("level_one_id = ?", levelOne.ID).First(&hierarchy.FdArticleVideo{}).Error, gorm.ErrRecordNotFound) {
		return errors.New("此目录有视频类型文章存在禁止删除")
	}
	if !errors.Is(global.GVA_DB.Where("level_one_id = ?", levelOne.ID).First(&hierarchy.FdArticleAudio{}).Error, gorm.ErrRecordNotFound) {
		return errors.New("此目录有音频类型文章存在禁止删除")
	}
	if !errors.Is(global.GVA_DB.Where("level_one_id = ?", levelOne.ID).First(&hierarchy.FdArticleGraphic{}).Error, gorm.ErrRecordNotFound) {
		return errors.New("此目录有图文类型文章存在禁止删除")
	}
	if err = global.GVA_DB.Delete(&levelOne).Error; err != nil {
		return errors.New("删除失败")
	}
	return
}

// DeleteLevelOneByIds 批量删除LevelOne记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelOneService *LevelOneService) DeleteLevelOneByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]hierarchy.LevelOne{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateLevelOne 更新LevelOne记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelOneService *LevelOneService) UpdateLevelOne(levelOne hierarchy.LevelOne) (err error) {
	err = global.GVA_DB.Save(&levelOne).Error
	return err
}

// GetLevelOne 根据id获取LevelOne记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelOneService *LevelOneService) GetLevelOne(id uint) (levelOne hierarchy.LevelOne, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&levelOne).Error
	return
}

// GetLevelOneInfoList 分页获取LevelOne记录
// Author [piexlmax](https://github.com/piexlmax)
func (levelOneService *LevelOneService) GetLevelOneInfoList(info hierarchyReq.LevelOneSearch) (list []hierarchy.LevelOne, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&hierarchy.LevelOne{})
	var levelOnes []hierarchy.LevelOne
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Key != "" {
		db = db.Where("`key` = ?", info.Key)
	}
	if info.Value != "" {
		db = db.Where("value = ?", info.Value)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Find(&levelOnes).Error
	return levelOnes, total, err
}

// GetLearnLevelOneInfoList 分页获取学习一级目录
// Author [g-qs](https://gitee.com/g-qs)
func (levelOneService *LevelOneService) GetLearnLevelOneInfoList(info hierarchyReq.LevelOneSearch) (list []hierarchy.LevelOne, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&hierarchy.LevelOne{}).Where("`key` IN (?)", "jmgy")
	var levelOnes []hierarchy.LevelOne
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Value != "" {
		db = db.Where("value = ?", info.Value)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Find(&levelOnes).Error
	return levelOnes, total, err
}
